package com.kltn.zigo.data.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.kltn.zigo.R;
import com.kltn.zigo.data.loader.UtilityLoader;

public class UtilityAdapter extends BaseAdapter{

	private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    private UtilityLoader utilityloader;
    ArrayList<String> selectedUtility;
    ArrayList<String> selectedUtilityID;
    
    public ArrayList<String> getSelectedUtility() {
        return selectedUtility;
    }

    public ArrayList<String> getSelectedUtilityID() {
        return selectedUtilityID;
    }
    
    public void setSelectedUtility(ArrayList<String> selectedUtility) {
        this.selectedUtility = selectedUtility;
    }
    
    public void setSelectedUtilityID(ArrayList<String> selectedUtilityID) {
        this.selectedUtilityID = selectedUtilityID;
    }
    
    public UtilityAdapter(Activity a, UtilityLoader ul) {
    	selectedUtility = new ArrayList<String>();
    	selectedUtilityID = new ArrayList<String>();
        activity = a;
        data=ul.GetUtility();
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        utilityloader = ul;        
    }
    
    public UtilityAdapter(Activity a, UtilityLoader ul, ArrayList<HashMap<String, String>> d) {
    	selectedUtility = new ArrayList<String>();
    	selectedUtilityID = new ArrayList<String>();
    	activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        utilityloader = ul;  
    }
	
	public int getCount() {
        return data.size();
	}

	public Object getItem(int position) {
        return position;
	}

	public long getItemId(int position) {
        return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item_list_utility, null);

        final TextView utility_id = (TextView)vi.findViewById(R.id.tvID);
        final CheckBox utility_name = (CheckBox)vi.findViewById(R.id.chb_Utility);
        
        HashMap<String, String> utility = new HashMap<String, String>();
        utility = data.get(position);
        // Setting all values in listview
        utility_id.setText(utility.get(utilityloader.TAG_UTILITY_ID));
        utility_name.setText(utility.get(utilityloader.TAG_UTILITY_NAME));
        
        utility_name.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					selectedUtility.add(utility_name.getText().toString());
					selectedUtilityID.add(utility_id.getText().toString());
				}
				else
				{
					selectedUtility.remove(utility_name.getText().toString());
					selectedUtilityID.remove(utility_id.getText().toString());
				}
			}
		});
        
        return vi;
	}
}
